package com.cy.yonghe.service.impl;

import com.cy.yonghe.mapper.DoorMapper;
import com.cy.yonghe.model.Door;
import com.cy.yonghe.service.IDoorService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class DoorServiceImpl implements IDoorService {

    @Autowired
    DoorMapper doorMapper;
    int pageSize = 5;

    @Override
    public List<Door> list(Integer pageNum,String name) {
        //对页码参数进行参数合法性检验
        if(Objects.isNull(pageNum)|| pageNum<1){
            pageNum = 1;
        }
        //使用pageHelper分页
        PageHelper.startPage(pageNum,pageSize);
        return doorMapper.selectList(name);
    }

    @Override
    public Door getById(Integer id) {
        return doorMapper.selectById(id);
    }

    @Override
    public void save(Door door) {
        //设置默认属性的值
        Date now = new Date();
        //1-营业中。 2-已停业
        door.setStatus(1);
        door.setCreatedTime(now);
        door.setUpdatedTime(now);
        doorMapper.insert(door);
    }

    @Override
    public void update(Door door) {
        //设置修改时间
        door.setUpdatedTime(new Date());

        doorMapper.update(door);
    }

    @Override
    public void delete(Integer id) {
        doorMapper.delete(id);
    }
}
